import Vue from 'vue'
import App from './App'
// main.js
import uView from 'uview-ui';
Vue.use(uView);

const verson = "1.0.0"; // 版本
const companyid = 0;
const host = "http://192.168.11.23:9006/api/"; // 服务器地址
// const host = "http://rb.345zh.com/api"; 
const very = 1;

const urls = {
	games: "/games/index", //游戏页面
	gamesmore:"/games/gamesmore"// 圈子分页
}

// 提示框
const msg = (title, duration = 2000, mask = false, icon = 'none') => {
	if (Boolean(title) === false) {
		return;
	}
	uni.showToast({
		title,
		duration,
		mask,
		icon
	});
}
// 将数据存放本地缓存中
const set = (name, data) => {
	try {
		uni.setStorageSync(name, data)
	} catch (e) {
		console.log(e)
	}
}
// 从本地缓存中获取指定 name 对应的内容
const get = (name) => {
	let data
	try {
		data = uni.getStorageSync(name)
	} catch (e) {
		console.log(e)
	}
	return data
}
// 删除缓存
const remove = (name) => {
	let data
	try {
		data = uni.removeStorageSync(name)
	} catch (e) {
		console.log(e)
	}
	return data
}
const navigateTo = (url) => {
	uni.navigateTo({
		url: url
	})
}

const request = (url, data = {}, m = 'POST') => {
	var token = get('token');
	console.log(companyid)
	data.token = token;
	data.pagesize = 20;
	data.companyid = companyid; // 修改头像
	data.v = very;
	return new Promise((reslove, reject) => {
		uni.request({
			url: host + url,
			method: m,
			data: data,
			header: {
				'content-type': 'application/x-www-form-urlencoded',
			},
			dataType: 'json',
			success: (d) => {
				console.log(d)
				if (d.statusCode !== 200) {
					msg('服务器错误')
					reject(new Error())
				} else {
					var code = parseInt(d.data.code);
					console.log(d.data);
					if (code == -2) {
						msg(d.data.msg);
						navigateTo(`/pages/lognIn/lognIn`);
					} else if (code == -1) {
						msg(d.data.msg);
						return;
					} else {
						reslove(d.data);
					}
				}
				//uni.hideLoading();
			}
		})
	})
}


Vue.config.productionTip = false

Vue.prototype.$api = {
	host,
	msg,
	urls,
	request,
	get,
	set,
	remove,
	navigateTo,
	companyid,
	very

}

App.mpType = 'app'

const app = new Vue({
	...App
})
app.$mount()
